package com.wmx.newspushmonitor;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.rhino.ui.utils.SharedPreferencesUtils;
import com.rhino.ui.utils.TimeUtils;
import com.rhino.ui.utils.okhttp.Callback;
import com.wmx.newspushmonitor.http.HttpUrl;
import com.wmx.newspushmonitor.util.LogWriter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * @author LuoLin
 * @since Create on 2019/1/17.
 */
public class MyPushReceiver extends BroadcastReceiver {

    private static final String TAG = "MyPushReceiver";
    public static final String ACTION_RECEIVE_PUSH = "android.intent.action.RECEIVE_PUSH";

    private OnReceivedPushListener mOnReceivedPushListener;

    public interface OnReceivedPushListener {
        void onReceived(String newsInfo, String packageName);
    }

    public MyPushReceiver(OnReceivedPushListener mOnReceivedPushListener) {
        this.mOnReceivedPushListener = mOnReceivedPushListener;
    }

    public static void sendMsg(Context context, String packageName, String title, String contentText) {
        Intent intent = new Intent();
        intent.setAction(ACTION_RECEIVE_PUSH);
        intent.putExtra("packageName", packageName);
        intent.putExtra("title", title);
        intent.putExtra("contentText", contentText);
        context.sendBroadcast(intent);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION_RECEIVE_PUSH.equals(action)) {
            String packageName = intent.getStringExtra("packageName");
            String title = intent.getStringExtra("title");
            String contentText = intent.getStringExtra("contentText");
            StringBuilder sb = new StringBuilder();
            sb.append("PackageName: ").append(packageName)
                    .append("\nTitle: ").append(title)
                    .append("\nContent: ").append(contentText);
            sb.append("\nTime: ").append(TimeUtils.formatTime(TimeUtils.getUtcMilliseconds(), "yyyy-MM-dd HH:mm:ss"));
            if (mOnReceivedPushListener != null) {
                mOnReceivedPushListener.onReceived(sb.toString(), packageName);
            }
            commitNewsInfoToServer(packageName, contentText);
        }
    }

    private void commitNewsInfoToServer(String packageName, String contentText) {
        if (SharedPreferencesUtils.getInstance().getString("token").isEmpty()) {
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("type", TextUtils.isEmpty(packageName) ? "未知" : packageName);
        map.put("content", TextUtils.isEmpty(contentText) ? "未知" : contentText);
        map.put("token", SharedPreferencesUtils.getInstance().getString("token"));
        NewsPushMonitorApplication.getHttpRequest().doPostByMap(HttpUrl.URL_LOGIN, map, new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                super.onResponse(call, response);
                LogWriter.i(TAG, "commit success!");
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                LogWriter.i(TAG, "commit error!");
            }
        });
    }

}